import tkinter as tk

def bubble_sort_asc(numbers):
    n = len(numbers)
    count = 0
    for i in range(n):
        for j in range(0, n - i - 1):
            count += 1
            if numbers[j] > numbers[j + 1]:
                numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
    print(f"bubble_sort 函数中的比较操作次数: {count}")
    return numbers

def bubble_sort_desc(numbers):
    n = len(numbers)
    for i in range(n):
        for j in range(0, n - i - 1):
            if numbers[j] < numbers[j + 1]:
                numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
    return numbers

def selection_sort_asc(numbers):
    for i in range(len(numbers)):
        min_idx = i
        for j in range(i + 1, len(numbers)):
            if numbers[min_idx] > numbers[j]:
                min_idx = j
        numbers[i], numbers[min_idx] = numbers[min_idx], numbers[i]
    return numbers

def selection_sort_desc(numbers):
    for i in range(len(numbers)):
        max_idx = i
        for j in range(i + 1, len(numbers)):
            if numbers[max_idx] < numbers[j]:
                max_idx = j
        numbers[i], numbers[max_idx] = numbers[max_idx], numbers[i]
    return numbers

def insertion_sort_asc(numbers):
    for i in range(1, len(numbers)):
        key = numbers[i]
        j = i - 1
        while j >= 0 and key < numbers[j]:
            numbers[j + 1] = numbers[j]
            j -= 1
        numbers[j + 1] = key
    return numbers

def insertion_sort_desc(numbers):
    for i in range(1, len(numbers)):
        key = numbers[i]
        j = i - 1
        while j >= 0 and key > numbers[j]:
            numbers[j + 1] = numbers[j]
            j -= 1
        numbers[j + 1] = key
    return numbers

def sort_numbers1():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_asc = bubble_sort_asc(numbers)
    output_label.config(text=f"冒泡排序（从小到大）: {sorted_numbers_asc}")

def sort_numbers2():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_asc = selection_sort_asc(numbers)
    output_label.config(text=f"选择排序（从小到大）: {sorted_numbers_asc}")

def sort_numbers3():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_asc = insertion_sort_asc(numbers)
    output_label.config(text=f"插入排序（从小到大）: {sorted_numbers_asc}")

def sort_numbers4():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_desc = bubble_sort_desc(numbers)
    output_label.config(text=f"冒泡排序（从大到小）: {sorted_numbers_desc}")

def sort_numbers5():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_desc = selection_sort_desc(numbers)
    output_label.config(text=f"选择排序（从大到小）: {sorted_numbers_desc}")

def sort_numbers6():
    input_text = entry.get()
    numbers = [int(num) for num in input_text.split()]
    sorted_numbers_desc = insertion_sort_desc(numbers)
    output_label.config(text=f"插入排序（从大到小）: {sorted_numbers_desc}")

# 创建主窗口
root = tk.Tk()
root.title("数字排序课程")
root.geometry("600x600")

# 输入框
entry_label = tk.Label(root, text="请输入数字，用空格分隔:")
entry_label.pack(pady=10)
entry = tk.Entry(root)
entry.pack(pady=10)

# 按钮框架，用于更好地组织排序按钮
lecture_button_frame = tk.Frame(root)
lecture_button_frame.pack(pady=10)

# 存储按钮
buttons = []

# 课程按钮
for i in range(1, 7):
    row = (i - 1) // 3 + 2  # 计算行号，从第3行开始，每3个按钮换一行
    col = (i - 1) % 3
    if i == 1:
        text = f"冒泡排序（从小到大）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers1,
                         width=20, height=1)
    elif i == 2:
        text = f"选择排序（从小到大）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers2,
                         width=20, height=1)
    elif i == 3:
        text = f"插入排序（从小到大）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers3,
                         width=20, height=1)
    elif i == 4:
        text = f"冒泡排序（从大到小）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers4,
                         width=20, height=1)
    elif i == 5:
        text = f"选择排序（从大到小）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers5,
                         width=20, height=1)
    elif i == 6:
        text = f"插入排序（从大到小）"
        button = tk.Button(lecture_button_frame, text=text,
                         command=sort_numbers6,
                         width=20, height=1)
    button.grid(row=row, column=col, padx=5, pady=5)
    buttons.append(button)

# 输出标签
output_label = tk.Label(root, text="")
output_label.pack(pady=10)

# 运行主界面
root.mainloop()